package org.example.springredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/stringTest")
    public String stringTest() {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set("k1", "111");
        ops.set("k2", "222");
        ops.set("k3", "333");
        System.out.println("hhh");
        System.out.println("k3: " + ops.get("k3"));
        return "OK";
    }

    @RequestMapping("/list")
    public String listTest() {

        ListOperations<String, String> ops = redisTemplate.opsForList();
        ops.leftPush("k1", "1");
        ops.leftPush("k1", "2");
        ops.leftPush("k1", "3");

        System.out.println("rightPop: " + ops.rightPop("k1"));
        System.out.println("rightPop: " + ops.rightPop("k1"));
        System.out.println("rightPop: " + ops.rightPop("k1"));
        System.out.println("rightPop: " + ops.rightPop("k1"));

        return "OK";
    }

    @GetMapping("/set")
    public String setTest() {
        SetOperations<String, String> ops = redisTemplate.opsForSet();

        ops.add("k1", "111");
        ops.add("k1", "222");
        ops.add("k1", "333");

        Set<String> values = ops.members("k1");
        System.out.println("values" + values);
        Long rem = ops.remove("k1", "111");
        System.out.println("remove: " + rem);
        Boolean result = ops.isMember("k1", "111");
        System.out.println("result: " + result);


        return "Pop" + ops.pop("k1");
    }

    @RequestMapping("hash")
    public String hashTest() {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();

        ops.put("k1", "f1", "111");
        ops.put("k1", "f2", "222");
        ops.put("k1", "f3", "333");
        Map<Object, Object> entries = ops.entries("k1");
        System.out.println("entries: " + entries);

        String f1 = (String) ops.get("k1", "f1");
        System.out.println("f1: " + f1);

        Long size = ops.size("k1");
        System.out.println("size: " + size);

        return "f2: " + ops.hasKey("k1", "f2");
    }

    @RequestMapping("zSet")
    public String zSetTest() {
        ZSetOperations<String, String> ops = redisTemplate.opsForZSet();

        ops.add("k1", "zhangsan", 10);
        ops.add("k1", "lisi", 20);
        ops.add("k1", "wangwu", 30);

        Set<String> members = ops.range("k1", 0, -1);
        System.out.println("result: " + members);
        Set<ZSetOperations.TypedTuple<String>> membersWithScore = ops.rangeWithScores("k1", 0, -1);
        System.out.println("membersWithScore: " + membersWithScore);
        Long remove = ops.remove("k1", "zhangsan");
        System.out.println("remove: " + remove);

        Double score = ops.score("k1", "lisi");
        System.out.println("score: " + score);

        return "lisi: rank = " + ops.rank("k1", "lisi");
    }
}
